package com.helloniu.fl.util {

import com.helloniu.fl.NCHandler;
import com.helloniu.fl.SOHandler;
import com.helloniu.fl.widget.HNCoreCompnent;
import flash.net.FileReference;
import flash.utils.ByteArray;
import flash.geom.Point;
import flash.media.Camera;
import flash.media.Microphone;
import flash.xml.XMLDocument;
import flash.xml.XMLNode;

import mx.controls.Alert;
import mx.controls.TextArea;

public class Tool {
    public function Tool() {
    }

    public static function setDebugArea(atextArea:TextArea):void {
        textArea = atextArea;
    }

    public static function getDebugArea():TextArea {
        return textArea;
    }

    public static function traceT(str:String):void {
        if(textArea == null) {
            return;
        }

        textArea.text = textArea.text + " \n " + str;
    }

    public static function alert(str:*):void {
        Alert.show("" + str);
    }

    public static function arrayCopy(arr:Array):Array {
        var newArr:Array = new Array();

        for(var i:int = 0; i < arr.length; i++) {
            newArr.push(arr[i]);
        }

        return newArr;
    }

    public static function toPointsArray(arr:Array):Array {
        var newArr:Array = new Array();

        for(var i:int = 0; i < arr.length; i++) {
            var ap:Point = new Point();
            ap.x = arr[i].x;
            ap.y = arr[i].y;
            newArr.push(ap);
        }

        return newArr;
    }

    public static function compareUsersArray(arr1:Array, arr2:Array):Boolean {
        return false;
    }

    public static function comparePointsArray(arr1:Array, arr2:Array):Boolean {
        if(arr1.length != arr2.length) {
            return false;
        }

        arr1 = toPointsArray(arr1);
        arr2 = toPointsArray(arr2);

        for(var i:int = 0; i < arr1.length; i++) {
            var p1:Point = arr1[i] as Point;
            var p2:Point = arr2[i] as Point;

            if(p1.x != p2.x || p1.y != p2.y) {
                return false;
            }
        }

        return true;
    }

    public static function arrayRemove(arr:Array, idx:int):Array {
        if(idx < 0 || idx > arr.length) {
            return arr;
        }

        var newArr:Array = new Array();

        for(var i:int = 0; i < arr.length; i++) {
            if(i == idx) {
                continue;
            }

            newArr.push(arr[i]);
        }

        return newArr;
    }

    // create a node like: <aname/>
    public static function creatXMLNode2(name:String):XMLNode {
        return (new XMLDocument("<" + name + "/>")).firstChild;
    }

    // create a node like: <aname/>
    public static function creatXMLNode3(name:String):XMLNode {
        var enode:XMLNode = new XMLNode(1, null); // element node
        enode.nodeName = name;
        return enode;
    }

    // create a node like: <aname>avalue</aname>
    public static function creatXMLNode(name:String, val:String):XMLNode {
        var enode:XMLNode = new XMLNode(1, null); // element node
        enode.nodeName = name;
        var tnode:XMLNode = new XMLNode(3, null); // text node
        tnode.nodeValue = val;
        enode.appendChild(tnode);

        return enode;
    }

    public static function initAV(microp:Microphone, camera:Camera):void {
        if(microp != null) {
            //
        }

        if(camera != null) {
            // @temp tonyy
            camera.setQuality(0, 100);
            // videoW, videoH
            camera.setMode(400, 300, 10);// set video width&height
        }
    }

	// to be removed
    public static function initConnection(comps:Array,ancHandler:NCHandler, asoHandler:SOHandler, auserID:String):void {
        for(var i:int = 0; i < comps.length; i++) {
            var comp:HNCoreCompnent = comps[i] as HNCoreCompnent;

            if(comp == null) {
                var e:Error = new Error("initConnection erro found!");
                Tool.alert("=====" + e.getStackTrace());
                continue;
            }

            comp.initConnection(ancHandler, asoHandler, auserID);
        }
    }

    // convert 0~100 int --> 0~1 float
    public static function toAlpha(num:int):Number {
        if(num < 0 || num > 100) {
            alert("error alpha value!");
        }

        return num/100;//0~1
    }

    /**
     * Generate the unique code for the selected file.
     */
    public static function getFileMD5ID(fileReference:FileReference):String  {
        return getDateUTC(fileReference.modificationDate) + "-" +
            getDateUTC(fileReference.creationDate) + "-" +
            fileReference.size + fileReference.type;
    }

    /**
     * Returns a string representation of the date.
     */
    private static function getDateUTC(date:Date):String  {
       return "" + date.fullYearUTC + date.dateUTC + date.dayUTC +
               date.hoursUTC + date.minutesUTC + date.secondsUTC +
               date.millisecondsUTC;
    }

    private static function getDateUTC2(date:Date):String  {
       var str:String = "";
       str = date.fullYearUTC + "-" + date.dateUTC + "-" + date.dayUTC + "-" +
           date.hoursUTC + "-" + date.minutesUTC + "-" + date.secondsUTC + "-" +
           date.millisecondsUTC;

           return str;
    }

    public static function readString(barr:ByteArray):String {
        var strl:int = barr.readInt();
        return barr.readUTFBytes(strl);
    }

    public static function readLong(barr:ByteArray):uint {
        var numStr:String = Tool.readString(barr);
        return parseInt(numStr) as uint;
    }

    public static function writeString(barr:ByteArray, str:String):void {
        barr.writeInt(str.length);
        barr.writeUTFBytes(str);
    }

    public static function writeLong(barr:ByteArray, num:uint):void {
        var str:String = num + "";
        writeString(barr, str);
    }
    
    private static var textArea:TextArea;
}
}